package com.offcn.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.offcn.bean.Blog;
import com.offcn.bean.Evaluate;
import com.offcn.bean.EvaluateExample;
import com.offcn.bean.User;
import com.offcn.mapper.BlogMapper;
import com.offcn.mapper.EvaluateMapper;
import com.offcn.mapper.UserMapper;
import com.offcn.service.EvaluateService;
import com.offcn.utils.OAResult;
import com.offcn.utils.PageView;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Resource
    private EvaluateMapper evaluateMapper;
    @Resource
    private BlogMapper blogMapper;
    @Resource
    private UserMapper userMapper;


    public Evaluate evaluateById(int bid) {
        EvaluateExample evaluateExample = new EvaluateExample();
        EvaluateExample.Criteria criteria = evaluateExample.createCriteria();
        criteria.andBFkEqualTo(bid);
        List<Evaluate> evaluateList = evaluateMapper.selectByExample(evaluateExample);
        for (Evaluate evaluate : evaluateList) {
            return evaluate;
        }
        return null;
    }

    public PageView<Evaluate> getAllEvaluate(int currentPage, Integer pageSize,String econtent) {
        PageHelper.startPage(currentPage,pageSize);
        EvaluateExample evaluateExample = new EvaluateExample();
        EvaluateExample.Criteria criteria = evaluateExample.createCriteria();
        if (econtent!=null&&!econtent.equals("")){
            criteria.andEcontentLike("%"+econtent+"%");
        }
        List<Evaluate> evaluateList = evaluateMapper.selectByExample(evaluateExample);
        for (Evaluate evaluate : evaluateList) {
            Blog blog = blogMapper.selectByPrimaryKey(evaluate.getbFk());
            User user = userMapper.selectByPrimaryKey(evaluate.getuFk());
            evaluate.setBlog(blog);
            evaluate.setUser(user);
        }
        PageInfo<Evaluate> pageInfo = new PageInfo<Evaluate>(evaluateList);
        PageView<Evaluate> pageView = new PageView<Evaluate>(currentPage,pageSize);
        pageView.setTotalrecords(pageInfo.getTotal());
        pageView.setRecords(pageInfo.getList());
        return pageView;
    }

    public OAResult deleteEvaluate(int eid) {
        int row = evaluateMapper.deleteByPrimaryKey(eid);
        if (row==1){
            return OAResult.ok(200,"删除成功");
        }
        return OAResult.ok(400,"删除失败");
    }
}
